package com.doodle.common.data;

import com.doodle.common.utils.EntrySchema;

import java.lang.reflect.Field;

public class BeanSchema extends EntrySchema {
	
	public BeanSchema(Class<? extends Bean> clazz) {
        super(clazz);
	}
    
    public String getString(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
    		Object obj = field.get(bean);
    		if (obj == null) {
    			return null;
    		} else {
    			return obj.toString();
    		}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    public boolean getBoolean(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			return field.getBoolean(bean);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return false;
    }
    
    public short getShort(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			return field.getShort(bean);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return 0;
    }
    
    public int getInt(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			return field.getInt(bean);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return 0;
    }
    
    public long getLong(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			return field.getLong(bean);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return 0;
    }
    
    public float getFloat(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			return field.getFloat(bean);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return 0;
    }
    
    public double getDouble(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			return field.getDouble(bean);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return 0;
    }
    
    public byte[] getBlob(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			return (byte[])field.get(bean);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    public boolean isNull(Bean bean, int index) {
    	Field field = mColumnInfo[index].field;
    	try {
			Object obj = field.get(bean);
			return  obj == null;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			return true;
		}
    	return true;
    }
}
